package com.goldcn.common;

import java.util.Calendar;
import java.util.Date;

/**
 * @Author Tim Peng
 * mail:  timpeng@163.com
 * Date: 2017/6/29
 * Time: 17:12
 * To change this template use File | Settings | File Templates.
 */
public class DateUtil {

    /**
     * 当天的开始时间
     * @return
     */
    public static long startOfTodDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date date=calendar.getTime();
        return date.getTime();
    }
    /**
     * 当天的结束时间
     * @return
     */
    public static long endOfTodDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        Date date=calendar.getTime();
        return date.getTime();
    }

    /**
     * 获取当月第一天
     * @param year
     * @param month 从0开始
     * @return 2017-02-01 00:00:00
     */
    public static Long firstDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.HOUR_OF_DAY, cal.getMinimum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, cal.getMinimum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND));
        cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
        return cal.getTimeInMillis();
    }

    /**
     * 获取当月最后一天
     * @param year
     * @param month 从0开始
     * @return 2017-02-28 23:59:59
     */
    public static Long lastDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.HOUR_OF_DAY, cal.getMaximum(Calendar.HOUR_OF_DAY));
        cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE));
        cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));
        cal.set(Calendar.DAY_OF_MONTH, cal.getMinimum(Calendar.DATE));
        cal.add(Calendar.MONTH, 1);
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTimeInMillis();
    }

    public static Long firstDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        return firstDayOfMonth(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH));
    }

    public static Long lastDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        return lastDayOfMonth(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH));
    }
}
